/**
 * 
 */
package com.suyx.job.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.suyx.job.config.BaiduOCR;
import com.suyx.job.config.RedisUtils;
import com.suyx.job.core.IHoldService;
import com.suyx.job.entity.AnimalInfo;
import com.suyx.job.entity.GoodsInfo;
import com.suyx.job.entity.LuaColorInfo;
import com.suyx.job.entity.Result;
import com.suyx.job.entity.RoleInfo;
import com.suyx.job.entity.TaskInfo;
import com.suyx.job.service.IAnimalInfoService;
import com.suyx.job.service.IGoodsInfoService;
import com.suyx.job.service.ILuaColorInfoService;
import com.suyx.job.service.IRoleInfoService;
import com.suyx.job.service.ITaskInfoService;
import com.suyx.job.service.area.IGoAreaService;
import com.suyx.job.service.sect.IGoToSectService;
import com.suyx.job.utils.ImagePHash;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.ScreenCapture;
import com.suyx.job.utils.SpeechUtil;
import com.suyx.job.utils.SysStaticData;
import com.suyx.job.utils.SysStaticPhData;

import lombok.extern.slf4j.Slf4j;

/**
 * @author syx
 */
@Slf4j
@RestController
@RequestMapping("/mhxyHundrSmTask")
public class MhxyHundrSmTaskController {

	@Autowired
	private IGoodsInfoService goodsService;

	@Autowired
	private IRoleInfoService roleService;

	@Autowired
	private BaiduOCR baiduOCR;

	@Autowired
	protected IHoldService holdService;
	@Autowired
	private IAnimalInfoService animalService;

	@Autowired
	private RedisUtils redisService;
	
	@Autowired
	private ILuaColorInfoService luaColorService;
	
	@Autowired
	private ITaskInfoService taskInfoService;

	/**
	 * 根据类对象获取服务实例对象
	 * 
	 * @param t
	 * @param <T>
	 * @return
	 * @throws Exception
	 */
	public <T> T getService(Class<T> t) throws Exception {
		return holdService.getServiceInstance(t);
	}

	public <T> T getService(String serviceName, Class<T> clazz) throws Exception {
		return holdService.getServiceInstance(serviceName);
	}

	//@PostMapping(value = "hundrSmTask")
	@RequestMapping("/hundrSmTask/{roleId}")
    public Result hundrSmTask(@PathVariable("roleId") Integer roleId) throws Exception{
		RoleInfo roleInfo = roleService.getOne(roleId);
		for(int i=0;i<30;i++){
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				SpeechUtil.textToSpeech(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			doSmTask(roleInfo);
			log.info(roleInfo.getRoleName() + "今天已经完成师门任务次数："+sectNum);
		}
		return new Result(true,"["+ roleInfo.getRoleName()+"] 师门任务完成");
	}

	private boolean doSmTask(RoleInfo roleInfo) throws Exception {
		String device = roleInfo.getDevice();
		String roleSect = roleInfo.getRoleSect();
		String path = SysStaticData.SC_PIC_PATH + device + "\\";
		String text =  getsmTaskInfo(roleInfo,path);
		if (StringUtils.isBlank(text) || text.indexOf("师") < 0) {
			log.info(roleInfo.getRoleName() + "已经没有可以做的师门任务,需要领取任务");
			holdService.getServiceInstances(
					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
					.forEach(s -> s.getTask(roleInfo, path, "1"));
			text =  getsmTaskInfo(roleInfo,path);
		}
		
		
		log.info(roleInfo.getRoleName() + "开始师门任务:" + text);

		String last_text = (String) redisService.get("last_text"+roleInfo.getRoleId());
		if(text.equals(last_text)){
			SpeechUtil.textToSpeech(roleInfo.getRoleName() + "当前任务在重复做");
			return false;
		}
		redisService.set("last_text"+roleInfo.getRoleId(), text,2400L);
		
		if (text.indexOf("任务完成") > -1) {
			// 回门派
			goBackHome(roleInfo, path, roleSect);
			// 点击师门任务寻ｅ
			clickSmTask(device,path);;
			Thread.sleep(SysStaticData.BACK_SM_TIME.get(roleSect) * 1000L);
			moveDialogBox(device);
			clickSmTask(device,path);;
			moveDialogBox(device);
		}

		if (text.indexOf("前往师父") > -1) {
			// 回门派
			goBackHome(roleInfo, path, roleSect);
			// 点击师门任务寻ｅ
			clickSmTask(device,path);
			Thread.sleep(SysStaticData.BACK_SM_TIME.get(roleSect) * 1000L);
			moveDialogBox(device);
		}

		// 示威
		if (text.indexOf("示威") > -1 || text.indexOf("威,") > -1) {
			
			wuYi(roleInfo,path);
			
            shiWei(text, path, roleInfo);
            
            Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0) + 1;
		    redisService.set(roleInfo.getRoleName(), sectNum,7200L);
		    
           //先回到门派再，领取任务
         	holdService.getServiceInstances(
					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
					.forEach(s -> s.getTask(roleInfo, path, "1"));
		}

		// 援救
		if (text.indexOf("同门") > -1) {
			wuYi(roleInfo,path);
			
            yuanJiu(text, path, roleInfo);
            
            Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0) + 1;
		    redisService.set(roleInfo.getRoleName(), sectNum);
		    
            //先回到门派再，领取任务
        	holdService.getServiceInstances(
					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
					.forEach(s -> s.getTask(roleInfo, path, "1"));
		}

		// 降服
		if (text.indexOf("乾坤袋") > -1) {
			while(true){
				wuYi(roleInfo,path);
				
				Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
				if(sectNum >= 20){
					log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
					break;
				}
                if(text.indexOf("将乾坤袋给师") > -1){
                	// 回师门 交任务
                	holdService.getServiceInstances(
        					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
        					.forEach(s -> s.finishTask(roleInfo, path, "1"));
                	
                	sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0) + 1;
        		    redisService.set(roleInfo.getRoleName(), sectNum);
        		    
                	// 直接领取任务
                	holdService.getServiceInstances(
        					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
        					.forEach(s -> s.getTask(roleInfo, path, "0"));
	            	break;
	            }
                
				xiangFu(text, path, roleInfo);
	            text =  getsmTaskInfo(roleInfo,path);     
			}

		}

		// 寻物
		if (text.indexOf("送给") > -1) {
            boolean isget = xunWu(text, path, roleInfo);
            if(isget){
            	// 回师门 交任务
            	holdService.getServiceInstances(
    					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
    					.forEach(s -> s.finishTask(roleInfo, path, "1"));
            	
            	Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0) + 1;
    		    redisService.set(roleInfo.getRoleName(), sectNum);
    		    
            	// 直接领取任务
            	holdService.getServiceInstances(
    					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
    					.forEach(s -> s.getTask(roleInfo, path, "0"));
            }
		}

		// 捕捉
		if (text.indexOf("五种属性") > -1) {
            buZhuo(text, path, roleInfo);
            
            // 回师门 交任务
        	holdService.getServiceInstances(
					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
					.forEach(s -> s.finishTask(roleInfo, path, "0"));
        	
        	Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0) + 1;
		    redisService.set(roleInfo.getRoleName(), sectNum);
		    
        	// 直接领取任务
        	holdService.getServiceInstances(
					IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
					.forEach(s -> s.getTask(roleInfo, path, "0"));
		}
     return false;
	}

	private String shiWei(String text, String path, RoleInfo roleInfo) throws Exception {
		String device = roleInfo.getDevice();
		for (String key : SysStaticData.AERA.keySet()) {
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			if("天机".indexOf(key) > -1){
				SpeechUtil.textToSpeech(roleInfo.getRoleName() + "开始去天机城示威");
			}
			
			if (text.indexOf(key) > -1) {
				dianXiang(roleInfo);
				
				holdService.getServiceInstances(IGoAreaService.class).stream()
						.filter(s -> s.support(SysStaticData.AERA.get(key)))
						.forEach(s -> s.execute(roleInfo.getDevice(), path, SysStaticData.AERA.get(key)));
				log.info(roleInfo.getRoleName() + "师门任务-援救: 到达" + key);
				// 使用天眼
				ScreenCapture.mouseClick(device, "1308 675");// # 打开包裹
				ScreenCapture.mouseClick(device, "1079 491");// 打开第四排第五个
				ScreenCapture.mouseClick(device, "532 448");// 使用
				ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹

				ScreenCapture.mouseClick(device, "1430 287");
				startGoto(roleInfo, path, "sw");

				break;
			}
		}
		return "";
	}

	private String yuanJiu(String text, String path, RoleInfo roleInfo) throws Exception {
		String device = roleInfo.getDevice();
		for (String key : SysStaticData.AERA.keySet()) {
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			
			if (text.indexOf(key) > -1) {
				if ("大唐".equals(key) && text.indexOf("大唐国境") > -1) {
					continue;
				}
				if ("大唐".equals(key) && text.indexOf("大唐境外") > -1) {
					continue;
				}
				if ("花果".equals(key) && text.indexOf("花果山") > -1) {
					continue;
				}
				sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
				if(sectNum >= 20){
					log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
					break;
				}
				dianXiang(roleInfo);
				
				holdService.getServiceInstances(IGoAreaService.class).stream()
						.filter(s -> s.support(SysStaticData.AERA.get(key)))
						.forEach(s -> s.execute(roleInfo.getDevice(), path, SysStaticData.AERA.get(key)));

				log.info(roleInfo.getRoleName() + "师门任务-援救: 到达" + key);
				clickSmTask(device,path);
				startGoto(roleInfo, path, "tm");
				break;
			}

		}
		return "";
	}

	private String xiangFu(String text, String path, RoleInfo roleInfo) throws Exception {
		String device = roleInfo.getDevice();
		for (String key : SysStaticData.SHOUFU_AERA.keySet()) {
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			
			if (text.indexOf(key) > -1) {
				if ("大唐".equals(key) && text.indexOf("大唐国境") > -1) {
					continue;
				}
				if ("大唐".equals(key) && text.indexOf("大唐境外") > -1) {
					continue;
				}
				if ("花果".equals(key) && text.indexOf("花果山") > -1) {
					continue;
				}
	
				// 购买乾坤袋
				ScreenCapture.mouseClick(device, "1308 675");// # 打开包裹
				Thread.sleep(500L);
				ScreenCapture.snapshot(device,path, "screen.png");
			    ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
			               new FileOutputStream(path + device +".png"), 774,273, 73, 66);//sm物品比较
				ImagePHash p = new ImagePHash();
			    String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			    String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH +"kong.png")));
			    String image3 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH +"qkd.png")));
			    int score =  p.distance(image1, image2);
			    int score3 =  p.distance(image1, image3);  
			    if(score3 < 5){
			    	ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
			    	score = 0;
			    }else{
			    	if("1".equals(roleInfo.getIsLianLing())){
			    		//有仙灵药铺5级
			    		ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
			    		ScreenCapture.mouseClick(device, "1211 682");
						ScreenCapture.mouseClick(device, "1201 590");
						
						ScreenCapture.mouseClick(device, "1344 469");//选择乾坤袋
						ScreenCapture.mouseClick(device, "1281 643");//购买
						ScreenCapture.mouseClick(device, "1389 52");//关闭仙灵
						
						ScreenCapture.mouseClick(device, "1211 682");
						
			    	}else{
			    		//==============罗道人购买乾坤袋======================
				    	ScreenCapture.mouseClick(device, "716 308");// 打开ca2 2-1
						ScreenCapture.mouseClick(device, "522 422");// 使用
						ScreenCapture.mouseClick(device, "694 538");// 购买乾坤袋
						ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹

						Thread.sleep(1 * 1000L);
						
						ScreenCapture.snapshot(device,path, "screen.png");
				        ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
				                new FileOutputStream(path + device +".png"), 15,409,49,37);
				        image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
				        image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH +"chuangkou.png")));
				        int score9 =  p.distance(image1, image2);
				        if(score9 == 0){
				        	ScreenCapture.mouseClick(device, "411 491");//点击缩放
				        }
				        // 屏蔽玩家
				        ScreenCapture.mouseClick(device, "39 249");
				        
						isTanwei(device, path);
						while (true) {
							// 点击罗道人
							ScreenCapture.mouseClick(device, "647 308");

							Thread.sleep(2 * 1000L);
							ScreenCapture.snapshot(device, path, "screen.png");
							ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
									new FileOutputStream(path + device + ".png"), 1228, 146, 30, 30);
							image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
							image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "daoren.png")));
							int score_over = p.distance(image1, image2);
							if (score_over == 0) {
								break;
							}

							if (sectNum >= 20) {
								log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
								break;
							}
						}

						ScreenCapture.mouseClick(device, "1220 142");
						Thread.sleep(1 * 1000L);
						ScreenCapture.mouseClick(device, "1350 132");
						Thread.sleep(1 * 1000L);
						ScreenCapture.mouseClick(device, "1167 646");
						Thread.sleep(1 * 1000L);
						ScreenCapture.mouseClick(device, "1391 55");
						ScreenCapture.mouseClick(device, "1391 55");

						// 解除屏蔽
						ScreenCapture.mouseClick(device, "47 656");//
						// ScreenCapture.mouseClick(device, "39 249");//
						// ScreenCapture.mouseClick(device, "387 670");
						// ==============罗道人购买乾坤袋======================
			    		
			    	}
			    	
			    	
			    }
			    
			    
			    
			    sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
				if(sectNum >= 20){
					log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
					break;
				}
				dianXiang(roleInfo);
				Thread.sleep(1 * 1000L);
				holdService.getServiceInstances(IGoAreaService.class).stream()
						.filter(s -> s.support(SysStaticData.SHOUFU_AERA.get(key)))
						.forEach(s -> s.execute(roleInfo.getDevice(), path, SysStaticData.SHOUFU_AERA.get(key)));

				log.info(roleInfo.getRoleName() + "师门任务-降服: 到达" + key);
				clickSmTask(device,path);
				startGotoForXiangFu(roleInfo,path,score);
                break;
			}
		}
		return "";
	}

	private String buZhuo(String text, String path, RoleInfo roleInfo) throws Exception {
		String annimal_type = null;
		String device = roleInfo.getDevice();
		for (String typeKey : SysStaticData.ANIMAL_TYPE.keySet()) {
			if (text.indexOf(typeKey) > -1) {
				annimal_type = typeKey;
				break;
			}
		}
		for (String animalName : SysStaticData.ANIMAL.keySet()) {
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			
			if (text.indexOf(animalName) > -1) {
				if ("黑熊".equals(animalName) && text.indexOf("黑熊精") > -1) {
					continue;
				}
				String[] infos = SysStaticData.CA1.get("长安商会").split("&");
				userCA1(device, infos);
				Thread.sleep(1 * 1000L);
				// 点击商会NPC
				ScreenCapture.mouseClick(device, "723 214");
				Thread.sleep(2 * 1000L);
				ScreenCapture.mouseClick(device, "1201 137");
				log.info(roleInfo.getRoleName() + "师门任务: 开始从商会选择召唤兽");
				List<AnimalInfo> sh_animalInfos = animalService.findByServerNameAndAnimalName(roleInfo.getRemark2(),
						SysStaticData.ANIMAL.get(animalName));
				Boolean overFlag = false;
				// 根据店铺号快速定位店铺
				ScreenCapture.mouseClick(device, "490 116");
				for (AnimalInfo sh_info : sh_animalInfos) {
					sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
					if(sectNum >= 20){
						log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
						break;
					}
					
					// 定位输入框
					ScreenCapture.mouseClick(device, "457 116");

					ScreenCapture.mouseClick(device, "750 223");
					ScreenCapture.mouseClick(device, "750 223");
					ScreenCapture.mouseClick(device, "750 223");
					// 在商会输入数字键盘上输入店铺号码
					input_sh(device, String.valueOf(sh_info.getDpCode()));
					// 确认
					ScreenCapture.mouseClick(device, "762 421");
					Thread.sleep(2 * 1000l);
					// 选择页面
					ScreenCapture.mouseClick(device, "176 638");
					Thread.sleep(1 * 1000l);
					ScreenCapture.mouseClick(device, sh_info.getLocation().replace("&", " "));
					String sb_zb_str = "183&193@416&190@183&258@421&284@185&380@419&380@185&468@416&474";
					String[] dp = sb_zb_str.split("@");
					for (int i = 0; i < dp.length; i++) {
						sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
						if(sectNum >= 20){
							log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
							break;
						}
						
						ScreenCapture.mouseClick(device, dp[i].replace("&", " "));
						Thread.sleep(1 * 1000l);
						if(isNUllAnimal(roleInfo, path, SysStaticData.ANIMAL.get(animalName), annimal_type)){
							break;
						}
						String flag = isOkAnimal(roleInfo, path, SysStaticData.ANIMAL.get(animalName), annimal_type);
						if ("2".equals(flag)) {
							break;
						}
						
						if ("0".equals(flag)) {
							// 点击购买
							ScreenCapture.mouseClick(device, "463 646");
							Thread.sleep(3500L);
							overFlag = true;
							break;
						}
					}
					// 关闭当前店铺页面
					ScreenCapture.mouseClick(device, "559 35");
					if (overFlag) {
						// 关闭商会页面
						Thread.sleep(1500L);
						ScreenCapture.mouseClick(device, "1206 48");
						break;
					}
				}
				log.info(roleInfo.getRoleName() + "师门任务: 从商会选择召唤兽结束");
				break;
			}
		}
		return "";

	}

	private boolean xunWu(String text, String path, RoleInfo roleInfo) throws Exception {
		String device = roleInfo.getDevice();
		for (String key : SysStaticData.WP.keySet()) {
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			
			if (text.indexOf(key) > -1) {
				GoodsInfo goodsInfo = goodsService.findByRoleIdAndGoodsName(roleInfo.getRoleId(),SysStaticData.WP.get(key).split("&")[0]);
				if (goodsInfo != null) {
					String location = goodsInfo.getLocation();
					String[] doclick_c_k_info = location.split("&");
					ScreenCapture.mouseClick(device, "1308 675");// # 打开包裹
					Thread.sleep(500L);
					ScreenCapture.mouseClick(device, "715 305");// 打开ca2 2-1
					Thread.sleep(500L);
					ScreenCapture.mouseClick(device, "522 422");// 使用
					ScreenCapture.mouseClick(device, "488 421");// 长安群雄仓库
					ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
					// 点击仓库NPC
					Thread.sleep(1 * 1000L);
					ScreenCapture.mouseClick(device, "664 292");
					Thread.sleep(1 * 1000L);
					ScreenCapture.mouseClick(device, "1228 442");

					// 选择仓库中的物品
					ScreenCapture.mouseClick(device, "344 624");
					ScreenCapture.mouseClick(device, doclick_c_k_info[0] + " " + doclick_c_k_info[1]);
					ScreenCapture.mouseClick(device, doclick_c_k_info[2] + " " + doclick_c_k_info[3]);
					ScreenCapture.mouseClick(device, doclick_c_k_info[4] + " " + doclick_c_k_info[5]);
					// 关闭仓库
					ScreenCapture.mouseClick(device, "1152 55");
					goodsInfo.setStatus("1");
					goodsService.saveAndFlush(goodsInfo);
                    return true;
				} else {
					String te = roleInfo.getRoleName() + "师门任务物品: " + SysStaticData.WP.get(key).split("&")[0] + "使用完毕";
					log.info(te);
					SpeechUtil.textToSpeech(te);
					//return false;
				}
				
				if(goodsInfo == null){
					//先去建业
					ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
					ScreenCapture.mouseClick(device, "1073 224");// 打开FF 1
					ScreenCapture.mouseClick(device, "527 406");// 使用
					//建邺城
					ScreenCapture.mouseClick(device, "890 435");
					ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
					Thread.sleep(4 * 1000L);
					
					String random_x = String.format("%.0f",Math.floor(400*Math.random()));
					ScreenCapture.mouseClick(device, random_x+" 103");
					
					ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
					
					//===============任务写入任务表============================================
					LuaColorInfo luaCorlorInfo = luaColorService.findByWpName(SysStaticData.WP.get(key).split("&")[0]);
					TaskInfo taskInfo = new TaskInfo();
					if(luaCorlorInfo != null ){
						if(roleInfo.getJqNo() != null){
							taskInfo.setCreateTime(new Date());
							taskInfo.setJqNo(roleInfo.getJqNo());
							taskInfo.setRoleId(String.valueOf(roleInfo.getRoleId()));
							taskInfo.setRoleName(roleInfo.getRoleName());
							taskInfo.setStatus("0");
							taskInfo.setWpType(Integer.valueOf(SysStaticData.WP.get(key).split("&")[2]));
							taskInfo.setWpName(SysStaticData.WP.get(key).split("&")[0]);
							taskInfo.setTaskType(1);
							taskInfo.setTaskText(text);
							// color & posandcolor & 是否从1仓库开始 &在机器上好友位置
							taskInfo.setRemark(luaCorlorInfo.getColor()+
									"&"+luaCorlorInfo.getPosandcolor()+
									"&"+luaCorlorInfo.getRemark()+
									"&"+roleInfo.getHyLoc());
							TaskInfo task = taskInfoService.findByRoleIdAndTaskText(taskInfo.getRoleId(), taskInfo.getTaskText());
							if(task ==  null){
								taskInfo = taskInfoService.saveAndFlush(taskInfo);
							}
						}else{
							log.info("角色没有配置机器编码信息："+roleInfo.getRoleName());
						}
					}else{
						log.info("物品没有配置LUA颜色信息："+SysStaticData.WP.get(key).split("&")[0]);
					}
					
					//==========================================================
					
					String wp = SysStaticData.WP.get(key).split("&")[0];
					Integer kongNum = getKongLocation(device, path, SysStaticData.WP.get(key).split("&")[0]);
					while(true){
						sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
						if(sectNum >= 20){
							log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
							ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
							break;
						}
						if(kongNum == 1){
							ScreenCapture.mouseClick(device, "716 402");
						}
						
						if(kongNum == 2){
							ScreenCapture.mouseClick(device, "804 301");
						}
						if(kongNum == 3){
							ScreenCapture.mouseClick(device, "898 301");
						}
						if(kongNum == 4){
							ScreenCapture.mouseClick(device, "998 301");
						}
						if(kongNum == 5){
							ScreenCapture.mouseClick(device, "1080 301");
						}
						
						ScreenCapture.snapshot(device, path, "screen.png");
						Thread.sleep(5 * 100L);
						ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 363,
								190, 153, 42);// y52 244 307
						String text2 = baiduOCR.generalBasic(path + device + ".png");// 33 535
						text2 = Optional.ofNullable(SysStaticPhData.PH_WQ_TO_CONVEN.get(text2)).orElse(text2);
						log.info("识别包裹物品："+text2);
						if(wp.equals(text2)){
							ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
							
							TaskInfo updateTask = taskInfoService.findByRoleIdAndTaskText(taskInfo.getRoleId(), taskInfo.getTaskText());
							if(updateTask != null){
								updateTask.setStatus("1");
								taskInfoService.saveAndFlush(updateTask);
							}
							
							return true;
						}
						
					}
					ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
				}
				
				
				
				String[] infos = SysStaticData.CA1.get("长安商会").split("&");
				userCA1(device, infos);
				Thread.sleep(1 * 1000L);
				while(true){// 点击商会NPC
					log.info(roleInfo.getRoleName() + "点击商会NPC");
					ScreenCapture.mouseClick(device, "723 214");
					ImagePHash p = new ImagePHash();
				    Thread.sleep(2 * 1000L);
					ScreenCapture.snapshot(device, path, "screen.png");
					ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
							new FileOutputStream(path + device + ".png"), 1301, 109, 30, 30);
					String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
					String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "shanghui.png")));
					int score_over = p.distance(image1, image2);
					if(score_over == 0 ){
						break;
					}
					
					if(sectNum >= 20){
						log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
						break;
					}
				}
				
				ScreenCapture.mouseClick(device, "1201 137");
				log.info(roleInfo.getRoleName() + "师门任务: 开始从商会选择物品");
				
				List<AnimalInfo> sh_Infos = animalService.findByServerNameAndAnimalName(roleInfo.getRemark2(),
						SysStaticData.WP.get(key).split("&")[0]);
				Boolean overFlag = false;
				// 根据店铺号快速定位店铺
				ScreenCapture.mouseClick(device, "490 116");
				for (AnimalInfo sh_info : sh_Infos) {
					sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
					if(sectNum >= 20){
						log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
						break;
					}
					
					String sb_zb_str = sh_info.getRemark2();
					if(StringUtils.isEmpty(sb_zb_str)){
						continue;
					}
					String[] dp = sb_zb_str.split("@");
					if(dp.length == 0){
						continue;
					}
					// 定位输入框
					ScreenCapture.mouseClick(device, "457 116");

					ScreenCapture.mouseClick(device, "750 223");
					ScreenCapture.mouseClick(device, "750 223");
					ScreenCapture.mouseClick(device, "750 223");
					// 在商会输入数字键盘上输入店铺号码
					input_sh(device, String.valueOf(sh_info.getDpCode()));
					// 确认
					ScreenCapture.mouseClick(device, "762 421");
					Thread.sleep(2 * 1000l);
					// 选择页面
					ScreenCapture.mouseClick(device, "176 638");
					Thread.sleep(1 * 1000l);
					ScreenCapture.mouseClick(device, sh_info.getLocation().replace("&", " "));
					
					for (int i = 0; i < dp.length; i++) {
						sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
						if(sectNum >= 20){
							log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
							break;
						}
						
						ScreenCapture.mouseClick(device, dp[i].replace("&", " "));
						Thread.sleep(1 * 1000l);
						if (isOkWp(roleInfo, path, SysStaticData.WP.get(key).split("&")[0],sh_info.getPrice())) {
							// 点击购买
							ScreenCapture.mouseClick(device, "463 646");
							Thread.sleep(3500L);
							overFlag = true;
							break;
						}
					}
					// 关闭当前店铺页面
					ScreenCapture.mouseClick(device, "559 35");
					if (overFlag) {
						// 关闭商会页面
						Thread.sleep(1500L);
						ScreenCapture.mouseClick(device, "1206 48");
						break;
					}
				}
				log.info(roleInfo.getRoleName() + "师门任务: 从商会选择物品结束");
				return overFlag;
			}
		}
		String te = roleInfo.getRoleName() + "师门任务: " + text + "无法完成完成师门任务";
		log.info(te);
		SpeechUtil.textToSpeech(te);
		return false;
	}

	protected void input_sh(String device, String dpCode) {
		for (int i = 0; i < dpCode.length(); i++) {
			char c = dpCode.charAt(i);
			String x_zb = SysStaticData.options_sh.get(String.valueOf(c));
			ScreenCapture.mouseClick(device, x_zb);
		}
	}

	protected void userCA1(String device, String[] infos) {
		ScreenCapture.mouseClick(device, "1308 675");// # 打开包裹
		ScreenCapture.mouseClick(device, "730 224");// 打开ca 1
		ScreenCapture.mouseClick(device, "522 422");// 使用
		ScreenCapture.mouseClick(device, infos[0] + " " + infos[1]);
		ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
	}

	// 获取师门任务信息
	private String getsmTaskInfo(RoleInfo roleInfo, String path) throws Exception {
		String device = roleInfo.getDevice();
		ScreenCapture.mouseClick(device, "1358 184");
		Thread.sleep(1 * 1000L);
		ScreenCapture.snapshot(device, path, "screen.png");
		Thread.sleep(5 * 100L);
		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 506,
				122, 612, 100);// y52 244 307
		String text = baiduOCR.generalBasic(path + device + ".png");// 33 535
																	// 1317 620
		/*String flag = "";
		if (text.indexOf("五种属性") > -1) {
			for (String animalName : SysStaticData.ANIMAL.keySet()) {
				Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
				if(sectNum >= 20){
					log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
					break;
				}
				
				if (text.indexOf(animalName) > -1) {
					flag = animalName;
					break;
				}
			}
		}
		
		if(StringUtils.isEmpty(flag)){
			text = baiduOCR.accurateBasic(path + device + ".png");
		}*/
		ScreenCapture.mouseClick(device, "1138 61");
		
		String xiang_time = (String) redisService.get("xiang_time"+roleInfo.getRoleId());
	    if (StringUtils.isEmpty(xiang_time)) {
			//使用摄药香
			ScreenCapture.mouseClick(device, "1308 675");// # 打开包裹
			ScreenCapture.mouseClick(device, "987 488");// # 
			ScreenCapture.mouseClick(device, "511 404");// # 
			ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
			redisService.set("xiang_time"+roleInfo.getRoleId(), "xiang_time",1680l);
		}
		return text;
	}
	
	
	

	// 去掉对话框
	private void moveDialogBox(String device) {
		ScreenCapture.mouseClick(device, "670 589");
	}

	//去掉签到任务
	private void clickSmTask(String device,String path) throws Exception {
		ScreenCapture.mouseClick(device, "1430 287");
	}

	// 判断屏蔽之后是否隐藏了主页面
	private boolean isyingCang(String device, String path) throws Exception, IOException {
		ScreenCapture.snapshot(device, path, "screen.png");
		ImagePHash p = new ImagePHash();

		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 26,
				545, 46, 40);// 隐藏
		String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "yingcang.png")));
		int score = p.distance(image1, image2);
		if (score == 0) {
			// 去掉
			ScreenCapture.mouseClick(device, "26 545");//
			return true;
		}
		return false;
	}

	// 判断屏蔽之后是否隐藏了摊位
	private boolean isTanwei(String device, String path) throws Exception, IOException {
		ScreenCapture.snapshot(device, path, "screen.png");
		ImagePHash p = new ImagePHash();

		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 33,
				453, 31, 56);
		String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "tanwei.png")));
		int score = p.distance(image1, image2);
		if (score == 0) {
			// 去掉
			ScreenCapture.mouseClick(device, "27 437");//
			return true;
		}
		return false;
	}

	// 判断是否是需要的召唤兽
		private boolean isNUllAnimal(RoleInfo roleInfo, String path, String animalName, String annimal_type) throws Exception {
			String device = roleInfo.getDevice();
			ScreenCapture.snapshot(device, path, "screen.png");
			ImagePHash p = new ImagePHash();

			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 720,
					636, 150, 43);//
			String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "is_animal_null.png")));
			int score = p.distance(image1, image2);
			// 判断当前商户页当前格子召唤兽是否卖空
			log.info(roleInfo.getRoleName() + "师门任务: 召唤兽是否卖空==>" + score);
			if (score == 0) {
				return false;
			}
			return true;
		}
		
	// 判断是否是需要的召唤兽
	private String isOkAnimal(RoleInfo roleInfo, String path, String animalName, String annimal_type) throws Exception {
		String device = roleInfo.getDevice();
		ScreenCapture.snapshot(device, path, "screen.png");
		ImagePHash p = new ImagePHash();

		/*ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 720,
				636, 150, 43);//
		String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "is_animal_null.png")));
		int score = p.distance(image1, image2);
		// 判断当前商户页当前格子召唤兽是否卖空
		log.info(roleInfo.getRoleName() + "师门任务: 召唤兽是否卖空==>" + score);
		if (score != 0) {
			return false;
		}*/

		// 判断是否是需要的召唤兽
		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 633,
				146, 65, 66);//
		String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + animalName + ".png")));
		int score = p.distance(image1, image2);
		log.info(roleInfo.getRoleName() + "师门任务: 是否是需要的召唤兽==>" + score);
		if (score > 3) {
			return "1";
		}
		// 判断价格是否过高
		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 119,
				528, 106, 23);
		String text = baiduOCR.generalBasic(path + device + ".png");
		log.info(roleInfo.getRoleName() + "师门任务: 价格是否过高==>" + text);
		if (StringUtils.isEmpty(text) || Integer.valueOf(text) > SysStaticData.MAX_PRICE_ANIMAL) {
			return "1";
		}

		// 判断是否是要找类型相同的
		Thread.sleep(5 * 100L);
		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 890,
				226, 51, 145);
		text = baiduOCR.getBestAnimal(path + device + ".png",roleInfo.getRoleName());
		if (StringUtils.isNoneBlank(text) && text.indexOf("yes") > -1) {
			log.info(roleInfo.getRoleName() + "师门任务: 需要换一个店铺");
			return "2";
		}
		
		if (StringUtils.isNoneBlank(text) && text.indexOf(annimal_type) > -1) {
			log.info(roleInfo.getRoleName() + "师门任务: 找到了需要的召唤兽==>" + text);
			return "0";
		}
		log.info(roleInfo.getRoleName() + "师门任务: 没有找到了需要的召唤兽，换下一个");
		return "1";
	}
	
	
	   // 判断是否是需要的物品
		private boolean isOkWp(RoleInfo roleInfo, String path, String wpName,Integer maxprice) throws Exception {
			String device = roleInfo.getDevice();
			ScreenCapture.snapshot(device, path, "screen.png");
			ImagePHash p = new ImagePHash();

			// 判断是否是需要的物品
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 635,162, 67, 63);//
			String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + wpName + ".png")));
			int score = p.distance(image1, image2);
			log.info(roleInfo.getRoleName() + "师门任务: 是否是需要的物品==>" + score);
			if (score <= 2) {
				// 判断价格是否过高
				ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 119,
						528, 106, 23);
				String text = baiduOCR.generalBasic(path + device + ".png");
				log.info(roleInfo.getRoleName() + "师门任务: 价格是否过高==>" + text);
				if (StringUtils.isNotEmpty(text) && Integer.valueOf(text) <= maxprice) {
					return true;
				}
				return false;
			}
			log.info(roleInfo.getRoleName() + "师门任务: 没有找到了需要的物品，换下一个");
			return false;
		}
		
		
		

	// 判断当前商户页当前格子召唤兽是否卖空 true = 卖空
	private boolean isNullAnimal(String device, String path) throws Exception {
		ScreenCapture.snapshot(device, path, "screen.png");
		ImagePHash p = new ImagePHash();

		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 720,
				636, 150, 43);//
		String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "is_animal_null.png")));
		int score = p.distance(image1, image2);
		if (score == 0) {
			return false;
		}
		return true;
	}

	private void goBackHome1(String device, String path) throws Exception {
		// =======================回门派========================
		while (true) {
			// # 加血 加蓝
			ScreenCapture.mouseClick(device, "1400 40");//
			ScreenCapture.mouseClick(device, "1193 109");// # 加血
			ScreenCapture.mouseClick(device, "1400 40");//
			ScreenCapture.mouseClick(device, "1359 109");// # 使用

			ScreenCapture.mouseClick(device, "1211 682");
			ScreenCapture.mouseClick(device, "1242 587");
			ScreenCapture.mouseClick(device, "1211 682");
			ScreenCapture.snapshot(device, path, "screen.png");
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"),
					93, 18, 117, 35);
			String area = baiduOCR.generalBasic(path + device + ".png");
			log.info("area:" + area);
			if (area.indexOf("凌波城") == -1) {
				ScreenCapture.mouseClick(device, "1211 682");
				ScreenCapture.mouseClick(device, "1242 587");
				ScreenCapture.mouseClick(device, "1211 682");
			} else {
				break;
			}

		}
		// =====================回门派=====================
	}

	private void goBackHome(RoleInfo roleInfo, String path, String roleSect) throws Exception {
		// =======================回门派========================
		String device = roleInfo.getDevice();
		while (true) {
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			
			ScreenCapture.snapshot(device, path, "screen.png");
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"),
					120, 48, 63, 36);
			ImagePHash p = new ImagePHash();
			String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "is_arrive_sm.png")));
			if ("dt".equals(roleSect)) {
				image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "dt_is_arrive_sm.png")));
			}
			if ("ps".equals(roleSect)) {
				image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "ps_is_arrive_sm.png")));
			}
			int score = p.distance(image1, image2);
			log.info(roleInfo.getRoleName() + "是否在回师门点：" + score);
			if (score > 0) {
				/*ScreenCapture.mouseClick(device, "1400 40");//
				ScreenCapture.mouseClick(device, "1193 109");// # 加血
				ScreenCapture.mouseClick(device, "1400 40");//
				ScreenCapture.mouseClick(device, "1359 109");// # 使用
*/
				ScreenCapture.mouseClick(device, "1211 682");
				ScreenCapture.mouseClick(device, "1264 618");
				ScreenCapture.mouseClick(device, "1211 682");
			} else {
				break;
			}

		}
		// =====================回门派=====================
	}

	public void startGoto(RoleInfo roleInfo, String path, String type) throws Exception {
		String device = roleInfo.getDevice();
		while (true) {
			ScreenCapture.snapshot(device, path, "screen.png");

			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + "_.png"),
					120, 48, 63, 36);

			Thread.sleep(2 * 1000L);
			ScreenCapture.snapshot(device, path, "screen.png");
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + "_1.png"),
					120, 48, 63, 36);
			ImagePHash p = new ImagePHash();
			String image1 = p.getHash(new FileInputStream(new File(path + device + "_.png")));
			String image2 = p.getHash(new FileInputStream(new File(path + device + "_1.png")));
			int score0 = p.distance(image1, image2);
			log.info("识别" + roleInfo.getRoleName() + "是否到达师门任务坐标点：" + score0);
			if (score0 == 0) {
				// 点击进入战斗
				if ("sw".equals(type)) {
					while(true){
						Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
						if(sectNum >= 20){
							log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
							break;
						}
						
					    Thread.sleep(2 * 1000L);
						ScreenCapture.snapshot(device, path, "screen.png");
						ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
								new FileOutputStream(path + device + ".png"), 1293, 413, 30, 30);
						image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
						image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "shiwei.png")));
						int score_over = p.distance(image1, image2);
						if(score_over == 0){
							break;
						}else{
							ScreenCapture.mouseClick(device, "1423 223");
						}
					}
					ScreenCapture.mouseClick(device, "1222 451");
				}
				if ("tm".equals(type)) {
					while(true){
						Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
						if(sectNum >= 20){
							log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
							break;
						}
						
					    Thread.sleep(2 * 1000L);
						ScreenCapture.snapshot(device, path, "screen.png");
						ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
								new FileOutputStream(path + device + ".png"), 1213, 357, 24, 24);
						image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
						image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "yuanzu.png")));
						int score_over = p.distance(image1, image2);
						if(score_over == 0){
							break;
						}else{
							ScreenCapture.mouseClick(device, "1423 223");
						}
					}
					
					ScreenCapture.mouseClick(device, "1224 353");
				}
				Thread.sleep(1 * 1000L);

				//ScreenCapture.mouseClick(device, "1390 681");// #自动
				//使用法术
				holdService.getServiceInstances(
						IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
						.forEach(s -> s.userfaShu(roleInfo, path));
				
				//判断战斗结束与否
				while (true) {
					Thread.sleep(2 * 1000L);
					ScreenCapture.snapshot(device, path, "screen.png");
					ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
							new FileOutputStream(path + device + "_zd.png"), 901, 660, 30, 30);
					image1 = p.getHash(new FileInputStream(new File(path + device + "_zd.png")));
					image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "zd_over_sm610.png")));
					/*if(roleInfo.getRoleId() == 609 || roleInfo.getRoleId() == 610){
						image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "zd_over_sm610.png")));
					}*/
					int score_over = p.distance(image1, image2);
					log.info(roleInfo.getRoleName() + "援助 是否战斗结束："+score_over);
					if (score_over < 10) {
						ScreenCapture.mouseClick(device, "670 589");
						// 取消自动自动
						ScreenCapture.mouseClick(device, "1242 558");// #
						break;
					}
				}	

				// # 加血
				ScreenCapture.mouseClick(device, "1400 40");//
				ScreenCapture.mouseClick(device, "1193 109");// # 加血
				ScreenCapture.mouseClick(device, "1400 40");//
				ScreenCapture.mouseClick(device, "1359 109");// # 使用
				ScreenCapture.mouseClick(device, "1258 33");//
				ScreenCapture.mouseClick(device, "992 103");// # 使用
				break;
			}
		}
	}
	
	public void startGotoForXiangFu(RoleInfo roleInfo, String path,int score_qk) throws Exception {
		String device = roleInfo.getDevice();
		while (true) {
			ScreenCapture.snapshot(device, path, "screen.png");
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + "_.png"),
					120, 48, 63, 36);

			Thread.sleep(3 * 1000L);
			ScreenCapture.snapshot(device, path, "screen.png");
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + "_1.png"),
					120, 48, 63, 36);
			ImagePHash p = new ImagePHash();
			String image1 = p.getHash(new FileInputStream(new File(path + device + "_.png")));
			String image2 = p.getHash(new FileInputStream(new File(path + device + "_1.png")));
			int score0 = p.distance(image1, image2);
			log.info("识别" + roleInfo.getRoleName() + "是否到达师门任务坐标点：" + score0);
			if (score0 == 0) {
				// 点击进入战斗
				while(true){
					Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
					if(sectNum >= 20){
						log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
						break;
					}
					
				    Thread.sleep(2 * 1000L);
					ScreenCapture.snapshot(device, path, "screen.png");
					ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
							new FileOutputStream(path + device + ".png"), 1266, 416, 30, 30);
					image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
					image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "shoufu.png")));
					int score_over = p.distance(image1, image2);
					if(score_over == 0){
						break;
					}else{
						ScreenCapture.mouseClick(device, "1401 230");
					}
				}
				ScreenCapture.mouseClick(device, "1222 451");
				
				Thread.sleep(1 * 1000L);
				//自动
				//ScreenCapture.mouseClick(device, "1390 681"); 
				//使用法术
				holdService.getServiceInstances(
						IGoToSectService.class).stream().filter(s -> s.support(roleInfo))
						.forEach(s -> s.userfaShuForA(roleInfo, path));
				
				// 取消自动自动
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(device, "1242 558");// #
				
				
				//判断战斗结束与否
				int i = 0;
				while (true) {
					i++;
					Thread.sleep(9 * 1000L);
					ScreenCapture.snapshot(device, path, "screen.png");
					ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
							new FileOutputStream(path + device + "_zd.png"), 901, 660, 30, 30);
					image1 = p.getHash(new FileInputStream(new File(path + device + "_zd.png")));
					image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "zd_over.png")));
					int score_over = p.distance(image1, image2);
					log.info("识别" + roleInfo.getRoleName() + "是否到达师门任务战斗是否结束：" + score_over);
					if (score_over < 10 ) {
						//ScreenCapture.mouseClick(device, "670 589");
						break;
					}else{
						//选着道具
						ScreenCapture.mouseClick(device, "1387 512");
						//选中乾坤袋
						if(score_qk == 0){
							ScreenCapture.mouseClick(device, "1077 378");
						}else{
							ScreenCapture.mouseClick(device, "1165 381");
						}
						
						//使用
						ScreenCapture.mouseClick(device, "1275 636");
						ScreenCapture.mouseClick(device, "558 200");
						ScreenCapture.mouseClick(device, "558 200");
					}
					
					if(i > 7){
						SpeechUtil.textToSpeech(roleInfo.getRoleName()+"师门收服不了，请手动处理");
						break;
					}
				}		

				// # 加血
				ScreenCapture.mouseClick(device, "1400 40");//
				ScreenCapture.mouseClick(device, "1193 109");// # 加血
				ScreenCapture.mouseClick(device, "1400 40");//
				ScreenCapture.mouseClick(device, "1359 109");// # 使用
				ScreenCapture.mouseClick(device, "1258 33");//
				ScreenCapture.mouseClick(device, "992 103");// # 使用
				break;
			}
		}
	}
	
	
	private void wuYi(RoleInfo roleInfo, String path) {
		String device = roleInfo.getDevice();
		ImagePHash p = new ImagePHash();
		ScreenCapture.snapshot(device, path, "screen.png");
		try {
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"),
					1074, 11, 42, 42);// 1074, 16, 35, 35

			String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "chong_wu.png")));
			int score9 = p.distance(image1, image2);
            log.info("是否需要巫医(不等于0需要巫医)："+score9);
			if (score9 > 2) {
				ScreenCapture.mouseClick(device, "1308 675");// # 打开包裹
				ScreenCapture.mouseClick(device, "892 222");
				ScreenCapture.mouseClick(device, "516 428");
				ScreenCapture.mouseClick(device, "510 518");
				ScreenCapture.mouseClick(device, "1145 52");

				//屏蔽按鈕
				ScreenCapture.mouseClick(device, "39 249");

				ScreenCapture.snapshot(device, path, "screen.png");
				ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
						new FileOutputStream(path + device + ".png"), 33, 453, 31, 56);
				image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
				image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "tanwei.png")));
				int score = p.distance(image1, image2);
				if (score == 0) {
					// 屏蔽摊位
					ScreenCapture.mouseClick(device, "27 437");//

				}
				// 点击巫医
				ScreenCapture.mouseClick(device, "627 291");
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(device, "1374 264");
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(device, "674 587");
				// 解除屏蔽
				ScreenCapture.mouseClick(device, "47 656");//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void dianXiang(RoleInfo roleInfo){
		String device = roleInfo.getDevice();
		String xiang_time = (String) redisService.get("xiang_time"+roleInfo.getRoleId());
	    if (StringUtils.isEmpty(xiang_time)) {
			//使用摄药香
			ScreenCapture.mouseClick(device, "1308 675");// # 打开包裹
			ScreenCapture.mouseClick(device, "987 488");// # 
			ScreenCapture.mouseClick(device, "511 404");// # 
			ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
			redisService.set("xiang_time"+roleInfo.getRoleId(), "xiang_time",1680l);
		}
	}
	
	private Integer getKongLocation(String device,String path,String wp) throws Exception{
		ScreenCapture.snapshot(device,path, "screen.png");
		   ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
		               new FileOutputStream(path + device +".png"), 774,273, 73, 66);//第2排2个
			ImagePHash p = new ImagePHash();
			String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "kong.png")));
			int score = p.distance(image1, image2);
			if(score < 8){
				return 2;
			}else{
				/*ScreenCapture.mouseClick(device, "815 402");
				
				ScreenCapture.snapshot(device, path, "screen.png");
				Thread.sleep(5 * 100L);
				ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 363,
						190, 153, 42);// y52 244 307
				String text2 = baiduOCR.generalBasic(path + device + ".png");// 33 535
				text2 = Optional.ofNullable(SysStaticPhData.PH_WQ_TO_CONVEN.get(text2)).orElse(text2);
				log.info("识别包裹物品："+text2);
				if(wp.equals(text2)){
					return 99;
				}*/
			}
			
			ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
		               new FileOutputStream(path + device +".png"), 864,273, 73, 66);//第2排3个
			String image3 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			score = p.distance(image3, image2);
			if(score < 8){
				return 3;
			}
			
			ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
		               new FileOutputStream(path + device +".png"), 954,273, 73, 66);//第2排4个
			String image4 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			score = p.distance(image4, image2);
			if(score < 8){
				return 4;
			}
			
			ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
		               new FileOutputStream(path + device +".png"), 1044,273, 73, 66);//第2排5个
			String image5 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			score = p.distance(image5, image2);
			if(score < 8){
				return 5;
			}
			
			ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
		               new FileOutputStream(path + device +".png"), 684,363, 73, 66);//第3排1个
			String image6 = p.getHash(new FileInputStream(new File(path + device + ".png")));
			score = p.distance(image6, image2);
			if(score < 8){
				return 1;
			}
			
		   return 0;
	   }
	
}
